package com.zxn.tablayoutsamples.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.zxn.tablayout.ScrollTabLayout;
import com.zxn.tablayout.SlidingTabLayout;
import com.zxn.tablayout.listener.CustomTabEntity;
import com.zxn.tablayout.listener.OnTabSelectListener;
import com.zxn.tablayoutsamples.R;
import com.zxn.tablayoutsamples.adapter.MyPagerAdapter;
import com.zxn.tablayoutsamples.entity.IConstantData;
import com.zxn.tablayoutsamples.entity.TabEntity;
import com.zxn.tablayoutsamples.utils.ViewFindUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * SlidingTabLayout选中某个条目滚至该位置并可见的效果、
 */
public class SlidingTabLayoutActivity extends AppCompatActivity {

    private ConstraintLayout mMain;
    private SlidingTabLayout mSlidingTabLayout;
    private ViewPager mViewPager;

    public static void jumpTo(Context context) {
        Intent intent = new Intent(context, SlidingTabLayoutActivity.class);
        context.startActivity(intent);
    }

    private final ArrayList<Fragment> mFragments = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_sliding_tab_layout);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        initView();

        String[] fruit = IConstantData.TITLE_FRUIT;
        for (String title : fruit) {
            mFragments.add(SimpleCardFragment.getInstance(title));
        }

        MyPagerAdapter mAdapter
                = new MyPagerAdapter(getSupportFragmentManager(), mFragments, fruit);
        mViewPager.setAdapter(mAdapter);
        mSlidingTabLayout.setViewPager(mViewPager);

        onInitTypeTabLayout();
    }

    private void initView() {
        mMain = findViewById(R.id.main);
        mSlidingTabLayout = findViewById(R.id.slidingTabLayout);
        mViewPager = findViewById(R.id.viewPager);

    }

    private void onInitTypeTabLayout() {
        ScrollTabLayout typeScrollTab = findViewById(R.id.scrollTab);
        List<CustomTabEntity> tabData = new ArrayList<>();
        for (String record : IConstantData.TITLE_FRUIT) {
            tabData.add(new TabEntity(record));
        }
        typeScrollTab.setTabData(tabData);
        typeScrollTab.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                //依靠ViewPager滚动的距离进行计算并进行滚动。
                //mSlidingTabLayout.setCurrentTab(position);
                mSlidingTabLayout.setCurrentTab(position, true);
            }

            @Override
            public void onTabReselect(int position) {

            }
        });
    }
}